﻿<?php

//////////////////////////////////////////////////////////////////////////
//
// WikiHiero2 - A PHP convert from text using "Manual for the encoding of
// hieroglyphic texts for computer input" syntax to HTML entities (table and
// images).
//
// Copyright (C) 2004-2010 Guillaume Blanchard (Aoineko)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
/**
 *
 */
class WH_Cleaner extends WH_Plugin
{
	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	function __construct()
	{
	}
	
	public static function CanModify() { return true; }
	
	public static function GetName() { return "Cleaner"; }
	public static function GetId() { return "Cleaner"; }
	public static function GetVersion() { return "0.1"; }
	public static function GetDesc($type = WH_Plugin::TypeDefault, $lang = "") { return "Internal format cleaner"; }
	public static function GetCredit($lang = "") { return "Guillaume Blanchard (2004-2010)"; }
	
	////////////////////////////////////////////////////////////////////////////////
   /**
	 *
	 */
	public function &Modify(&$document)
	{
		$this->CleanupChild($document->GetRoot());
		
		sort($document->GetRoot()->GetChildren());

		return $document;
	}

	////////////////////////////////////////////////////////////////////////////////
   /**
	 *
	 */
	private function CleanupChild(&$node)
	{
		switch($node->GetClass())
		{
			case WH_CLASS_HIEROGLYPH:
			case WH_CLASS_FRAME:
			case WH_CLASS_GROUP:
			case WH_CLASS_SECTION:
				return false;

			/*case WH_CLASS_TEXT:
				if($node->GetAttribute("type") != WH_TEXT_HIEROGLYPH)
					if($node->GetAttribute("content") != "")
						return false;
				break;*/
		}
		//if(count($node->children) == 0)
		if($node->GetChildNumber() == 0)
			return true;

		$empty = true;
		foreach($node->GetChildren() as $i => $child)
		{
			//if($this->CleanupChild($node->children[$i]))
			if($this->CleanupChild($node->GetChild($i)))
				$node->RemoveChild($i);
			else
				$empty = false;
		}

		return $empty;
	}
}

?>